#include "RiotShield.h"

char *paddleFile = "RiotShield.mesh";

RiotShield::RiotShield(char* name, Ogre::SceneManager* sm, Ogre::SceneNode* toAddTo, bool doIAdd) : upPressed(0), downPressed(0), rightPressed(0), leftPressed(0) {
	physical = doIAdd;
	if(doIAdd){
		Ogre::Entity* paddle;
		paddle = sm->createEntity(name, paddleFile);
		myNode = toAddTo->createChildSceneNode();
		Ogre::SceneNode* tmpNode = myNode->createChildSceneNode();
		tmpNode->setScale(40,40,40);
		tmpNode->pitch(Ogre::Degree(90));
		tmpNode->attachObject(paddle);
		myNode->setPosition(myPos.x, myPos.y, myPos.z);
	}
}

//change velocity, and move
void RiotShield::updateState(){
	runLogic();
	if(myPos.vy > SMAXVY){
		myPos.vy = SMAXVY;
	}
	if(myPos.vy < SMINVY){
		myPos.vy = SMINVY;
	}
	if(myPos.vx > SMAXVX){
		myPos.vx = SMAXVX;
	}
	if(myPos.vx < SMINVX){
		myPos.vx = SMINVX;
	}
	myPos.updatePosition();
	if((myPos.y) > (MAXY - SHIELDU)){
		myPos.y = (MAXY - SHIELDU);
		myPos.vy = (myPos.vy > 0) ? 0 : myPos.vy;
	}
	if((myPos.y) < (MINY - SHIELDD)){
		myPos.y = (MINY - SHIELDD);
		myPos.vy = (myPos.vy < 0) ? 0 : myPos.vy;
	}
	if((myPos.x) > (MAXX - SHIELDR)){
		myPos.x = (MAXX - SHIELDR);
		myPos.vx = (myPos.vx > 0) ? 0 : myPos.vx;
	}
	if((myPos.x) < (MINX - SHIELDL)){
		myPos.x = (MINX - SHIELDL);
		myPos.vx = (myPos.vx < 0) ? 0 : myPos.vx;
	}
	myPos.z = 0;
	myPos.vz = 0;
	if(physical){
		myNode->setPosition(myPos.x, myPos.y, myPos.z);
		//std::cout << "Writing POS " << myPos.x << " " << myPos.y << " " << myPos.z << "\n";
	}
}

//make a note of what has been pressed
void RiotShield::alertToInput( const InputSet& input ){
	upPressed = 0;
	downPressed = 0;
	rightPressed = 0;
	leftPressed = 0;
	if(input.get( IS_U )){
		upPressed = 1;
	}
	if(input.get( IS_D )){
		downPressed = 1;
	}
	if(input.get( IS_L )){
		leftPressed = 1;
	}
	if(input.get( IS_R )){
		rightPressed = 1;
	}
}

void RiotShield::getBoundingBox(double* lx, double* ly, double* hx, double* hy){
	*lx = myPos.x + SHIELDL;
	*ly = myPos.y + SHIELDD;
	*hx = myPos.x + SHIELDR;
	*hy = myPos.y + SHIELDU;
}
